package com.imedway.ihe.dtz.xds.registry.sub;

import java.util.List;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryError;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryError30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryResponse30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLSubmitObjectsRequest30;
import org.openehealth.ipf.commons.ihe.xds.core.responses.ErrorCode;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Severity;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
@DependsOn(value={"iti42SubRegistry"})
public class Iti61SubRegistry {
	
	@Autowired
	private Iti42SubRegistry iti42SubRegistry;
	
	public void run(EbXMLRegistryResponse30 ebXMLRegistryResponse30,EbXMLSubmitObjectsRequest30 ebXMLBody) {

		List<EbXMLRegistryError> registryError = ebXMLRegistryResponse30.getErrors();
		try {		
			
			iti42SubRegistry.run(ebXMLRegistryResponse30, ebXMLBody);
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
			genError(registryError, Severity.ERROR, ErrorCode.INVALID_REQUEST_EXCEPTION.getOpcode(),
					e.getMessage());	
		}
				
	}
	private void genError(List<EbXMLRegistryError> registryError,Severity severity,String errorCode,String codeContext) {
		EbXMLRegistryError30 re = new EbXMLRegistryError30(new RegistryError());
		re.setSeverity(severity);
		re.setErrorCode(errorCode);
		re.setCodeContext(codeContext);
		registryError.add(re);
	}

}
